package com.haixiaoke.business;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson2.JSONObject;
import com.haixiaoke.common.core.domain.AjaxResult;
import com.haixiaoke.common.core.page.TableDataInfo;
import com.haixiaoke.common.exception.ServiceException;
import com.haixiaoke.common.utils.poi.ExcelUtil;
import com.haixiaoke.domain.SmartLock;
import com.haixiaoke.domain.SmartLockBluetooth;
import com.haixiaoke.service.IBrandService;
import com.haixiaoke.service.ISmartLockService;
import com.haixiaoke.smartLock.haixiaoke.SmartLockHXKEventHandleService;
import com.haixiaoke.smartLock.haixiaoke.SmartLockHXKService;
import com.haixiaoke.smartLock.huixiangjia.SmartLockHxjEventHandleService;
import com.haixiaoke.smartLock.tianwanghulian.SmartLockTwhlEventHandleService;
import com.haixiaoke.smartLock.yaluete.SmartLockYLTEventService;
import com.haixiaoke.smartLock.youdian.SmartLockYdEventHandleService;
import com.haixiaoke.system.controller.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 智能锁设备Controller
 *
 * @author xiaolong
 * @date 2023-05-25
 */
@RestController
@RequestMapping("/business/lock")
public class SmartLockController extends BaseController {

    @Autowired
    private ISmartLockService smartLockService;

    @Autowired
    private IBrandService brandService;

    @Autowired
    private SmartLockYLTEventService smartLockYLTEventService;

    @Autowired
    private SmartLockYdEventHandleService smartLockYdEventHandleService;

    @Autowired
    private SmartLockTwhlEventHandleService smartLockTwhlEventHandleService;

    @Autowired
    private SmartLockHXKEventHandleService smartLockHXKEventHandleService;

    @Autowired
    private SmartLockHxjEventHandleService smartLockHxjEventHandleService;

    @Autowired
    private SmartLockHXKService smartLockHXKService;

    /**
     * 查询智能锁设备列表
     */
    @GetMapping("/list")
    public TableDataInfo list(SmartLock smartLock) {
        startPage();
        List<SmartLock> list = smartLockService.selectSmartLockList(smartLock);
        return getDataTable(list);
    }

    /**
     * 查询智能锁设备列表
     */
    @PostMapping("/listBySns")
    public AjaxResult listBySns(@RequestBody List<String> sns) {
        if (CollectionUtil.isEmpty(sns)) {
            return error("sn不能为空");
        }
        List<SmartLock> list = smartLockService.selectSmartLockListBySns(sns);
        return success(list);
    }

    /**
     * 查询第三方智能锁信息
     */
    @PostMapping("/listAll")
    public Map<String, String> listAll(@RequestBody SmartLock smartLock) {
        Map<String, String> map = new HashMap<>();
        String result = smartLockHXKService.getSmartLock(smartLock);
        JSONObject resultObject = JSONObject.parseObject(result);
        String smartLockHXK = resultObject.getString("result");
        map.put("smartLockHXK", smartLockHXK);
        return map;
    }

    @PostMapping("/hxkHouse/homePage")
    public AjaxResult homePage(@RequestBody List<String> sns) {
        if (CollectionUtil.isEmpty(sns)) {
            return error("sn不能为空");
        }
        Map<String, Object> map = smartLockService.homePage(sns);
        return success(map);
    }

    /**
     * 导出智能锁设备列表
     */
    @PostMapping("/export")
    public void export(HttpServletResponse response, SmartLock smartLock) {
        List<SmartLock> list = smartLockService.selectSmartLockList(smartLock);
        ExcelUtil<SmartLock> util = new ExcelUtil<SmartLock>(SmartLock.class);
        util.exportExcel(response, list, "智能锁设备数据");
    }

    /**
     * 获取智能锁设备详细信息
     */
    @GetMapping(value = "/{lockId}")
    public AjaxResult getInfo(@PathVariable("lockId") Long lockId) {
        return success(smartLockService.selectSmartLockByLockId(lockId));
    }

    /**
     * 新增智能锁设备
     */
    @PostMapping
    public AjaxResult add(@RequestBody SmartLock smartLock) {
        if (StrUtil.isEmpty(smartLock.getSn())) {
            return error("请输入智能锁SN号");
        }
        if (!smartLockService.checkLockSnUnique(smartLock.getSn())) {
            return error("新增智能锁设备'" + smartLock.getSn() + "'失败,设备已存在");
        }
        if (smartLock.getBrandId() == SmartLock.THIRD_GENERATION_SMART_LOCK) {
            return toAjax(smartLockYLTEventService.insertSmartLock(smartLock));
        } else if (smartLock.getBrandId() == SmartLock.FOUR_GENERATION_SMART_LOCK) {
            return toAjax(smartLockTwhlEventHandleService.insertSmartLock(smartLock));
        } else if (smartLock.getBrandId() == SmartLock.FIVE_GENERATION_SMART_LOCK) {
            return toAjax(smartLockHXKEventHandleService.insertSmartLock(smartLock));
        } else if (smartLock.getBrandId() == SmartLock.SIX_GENERATION_SMART_LOCK) {
            return toAjax(smartLockYdEventHandleService.insertSmartLock(smartLock));
        } else if (smartLock.getBrandId() == SmartLock.SEVEN_GENERATION_SMART_LOCK) {
            return toAjax(smartLockHxjEventHandleService.insertSmartLock(smartLock));
        } else {
            return AjaxResult.error("设备品牌不支持");
        }


    }

    /**
     * 修改智能锁设备
     */
    @PutMapping
    public AjaxResult edit(@RequestBody SmartLock smartLock) {
        return toAjax(smartLockService.updateSmartLock(smartLock));
    }

    /**
     * 删除智能锁设备
     */
    @DeleteMapping("/{lockIds}")
    public AjaxResult remove(@PathVariable Long[] lockIds) {
        Integer count = 0;
        for (Long lockId : lockIds) {
            SmartLock smartLock = smartLockService.selectSmartLockByLockId(lockId);
            if (smartLock.getBrandId() == SmartLock.THIRD_GENERATION_SMART_LOCK) {
                count += smartLockYLTEventService.removeSmartLock(smartLock);
            } else if (smartLock.getBrandId() == SmartLock.FOUR_GENERATION_SMART_LOCK) {
                count += smartLockTwhlEventHandleService.deleteSmartLockByLockId(smartLock);
            } else if (smartLock.getBrandId() == SmartLock.FIVE_GENERATION_SMART_LOCK) {
                count += smartLockHXKEventHandleService.deleteSmartLockByLockId(smartLock);
            } else if (smartLock.getBrandId() == SmartLock.SIX_GENERATION_SMART_LOCK){
                count += smartLockYdEventHandleService.deleteSmartLockByLockId(smartLock);
            } else if (smartLock.getBrandId() == SmartLock.SEVEN_GENERATION_SMART_LOCK){
                count += smartLockHxjEventHandleService.deleteSmartLockByLockId(smartLock);
            } else {
                throw new ServiceException("设备品牌不支持");
            }

        }
        if (lockIds.length != count) {
            throw new ServiceException("删除设备异常!");
        } else {
            return success();
        }
    }

    /**
     * 删除智能锁设备
     */
    @DeleteMapping("/sn/{sn}")
    public AjaxResult remove(@PathVariable String[] sn) {
        for (String lockId : sn) {
            SmartLock smartLock = smartLockService.selectBySn(lockId);
            if (ObjectUtil.isEmpty(smartLock)) {
                continue;
            }
            if (smartLock.getBrandId() == SmartLock.THIRD_GENERATION_SMART_LOCK) {
                smartLockYLTEventService.removeSmartLock(smartLock);
            } else if (smartLock.getBrandId() == SmartLock.FOUR_GENERATION_SMART_LOCK) {
                smartLockTwhlEventHandleService.deleteSmartLockByLockId(smartLock);
            } else if (smartLock.getBrandId() == SmartLock.FIVE_GENERATION_SMART_LOCK) {
                smartLockHXKEventHandleService.deleteSmartLockByLockId(smartLock);
            } else if (smartLock.getBrandId() == SmartLock.SIX_GENERATION_SMART_LOCK){
                smartLockYdEventHandleService.deleteSmartLockByLockId(smartLock);
            } else if (smartLock.getBrandId() == SmartLock.SEVEN_GENERATION_SMART_LOCK){
                smartLockHxjEventHandleService.deleteSmartLockByLockId(smartLock);
            } else {
                throw new ServiceException("设备品牌不支持");
            }

        }
        return success();
    }

    /**
     * 获取智能锁门卡信息
     *
     * @param sn 智能锁SN
     * @return
     */
    @GetMapping(value = "/card/{sn}")
    public AjaxResult getCard(@PathVariable("sn") String sn) {
        SmartLock smartLock = smartLockService.selectBySn(sn);
        if (smartLock.getBrandId() == SmartLock.THIRD_GENERATION_SMART_LOCK) {
            return success(smartLockYLTEventService.selectLockCard(sn));
        } else if (smartLock.getBrandId() == SmartLock.FOUR_GENERATION_SMART_LOCK) {
            return success(smartLockService.selectSmartLockBySn(sn));
        } else if (smartLock.getBrandId() == SmartLock.FIVE_GENERATION_SMART_LOCK) {
            return success(smartLockHXKEventHandleService.selectSmartLockBySn(sn));
        } else if (smartLock.getBrandId() == SmartLock.SIX_GENERATION_SMART_LOCK) {
            return success(smartLockYdEventHandleService.selectLockCard(sn));
        } else if (smartLock.getBrandId() == SmartLock.SEVEN_GENERATION_SMART_LOCK) {
            return success(smartLockHxjEventHandleService.selectLockCard(sn));
        } else {
            return AjaxResult.error("无门卡信息");
        }
    }

    /**
     * 获取智能锁临时密码权限信息
     *
     * @param sn 智能锁SN
     * @return
     */
    @GetMapping(value = "/password/{sn}")
    public AjaxResult getPassword(@PathVariable("sn") String sn) {
        SmartLock smartLock = smartLockService.selectBySn(sn);
        if (smartLock.getBrandId() == SmartLock.THIRD_GENERATION_SMART_LOCK) {
            return success(smartLockYLTEventService.getLockPassword(sn));
        } else if (smartLock.getBrandId() == SmartLock.FOUR_GENERATION_SMART_LOCK) {
            return success(smartLockService.getLockPassword(sn));
        } else if (smartLock.getBrandId() == SmartLock.FIVE_GENERATION_SMART_LOCK) {
            return success(smartLockHXKEventHandleService.getLockPassword(sn));
        } else if (smartLock.getBrandId() == SmartLock.SIX_GENERATION_SMART_LOCK) {
            return success(smartLockYdEventHandleService.getLockPassword(sn));
        } else if (smartLock.getBrandId() == SmartLock.SEVEN_GENERATION_SMART_LOCK) {
            return success(smartLockHxjEventHandleService.getLockPassword(sn));
        } else {
            return AjaxResult.error("无临时密码权限信息");
        }
    }

    /**
     * 获取智能锁临时蓝牙权限信息
     *
     * @param sn 智能锁SN
     * @return
     */
    @GetMapping(value = "/bluetooth/{sn}")
    public AjaxResult getBluetooth(@PathVariable("sn") String sn) {
        SmartLock smartLock = smartLockService.selectBySn(sn);
        if (smartLock.getBrandId() == SmartLock.THIRD_GENERATION_SMART_LOCK) {
            return success(smartLockYLTEventService.getBluetooth(sn));
        } else if (smartLock.getBrandId() == SmartLock.FOUR_GENERATION_SMART_LOCK) {
            return success(smartLockService.getBluetooth(sn));
        } else if (smartLock.getBrandId() == SmartLock.SEVEN_GENERATION_SMART_LOCK) {
            return success(smartLockHxjEventHandleService.getBluetooth(sn));
        } else {
            return AjaxResult.error("无蓝牙授权信息");
        }
    }

    /**
     * 删除智能锁蓝牙权限
     *
     * @param sn     智能锁SN
     * @param mobile 蓝牙标识
     * @return
     */
    @DeleteMapping(value = "/deleteBle")
    public AjaxResult deleteBle(@RequestParam(value = "sn") String sn,
                                @RequestParam(value = "mobile") String mobile) {
        SmartLock smartLock = smartLockService.selectBySn(sn);
        if (smartLock.getBrandId() == SmartLock.THIRD_GENERATION_SMART_LOCK) {
            SmartLockBluetooth smartLockBluetooth = new SmartLockBluetooth();
            smartLockBluetooth.setSn(sn);
            smartLockBluetooth.setIdentification(mobile);
            return toAjax(smartLockYLTEventService.deleteSmartLockBluetooth(smartLockBluetooth));
        } else if (smartLock.getBrandId() == SmartLock.FOUR_GENERATION_SMART_LOCK) {
            return toAjax(smartLockService.deleteBle(sn, mobile));
        } else if (smartLock.getBrandId() == SmartLock.SEVEN_GENERATION_SMART_LOCK) {
            return toAjax(smartLockHxjEventHandleService.deleteBle(sn, mobile));
        } else {
            return AjaxResult.error("该设备没有蓝牙授权信息");
        }
    }

    /**
     * 生产离线密码
     *
     * @param smartLock 智能锁设备对象
     * @return
     */
    @PostMapping(value = "/offlinePassword")
    public AjaxResult offlinePassword(@RequestBody SmartLock smartLock) {
        SmartLock smartLock1 = smartLockService.selectBySn(smartLock.getSn());
        if (smartLock1.getBrandId() == SmartLock.THIRD_GENERATION_SMART_LOCK) {
            return error("三代智能锁不支持该功能");
        } else if (smartLock1.getBrandId() == SmartLock.FOUR_GENERATION_SMART_LOCK) {
            return success(smartLockTwhlEventHandleService.offlinePassword(smartLock));
        } else if (smartLock1.getBrandId() == SmartLock.FIVE_GENERATION_SMART_LOCK) {
            AjaxResult ajaxResult = smartLockHXKEventHandleService.offlinePwd(smartLock);
            Object pwd = ajaxResult.get("msg");
            Map<String, Object> map = new HashMap<>();
            map.put("password", pwd);
            return success(map);
        }else {
            return AjaxResult.error("该设备没有离线密码功能");
        }
    }

    /**
     * 添加胁迫报警密码
     *
     * @param smartLock 智能锁设备对象
     * @return
     */
    // todo 空实现，厂商没有这个接口
    @PostMapping(value = "/coercionPassword")
    public AjaxResult coercionPassword(@RequestBody SmartLock smartLock) {
        SmartLock smartLock1 = smartLockService.selectBySn(smartLock.getSn());
        if (smartLock1.getBrandId() == SmartLock.THIRD_GENERATION_SMART_LOCK) {
            return error("三代智能锁不支持该功能");
        } else {
            return success(smartLockService.coercionPassword(smartLock));
        }
    }

    /**
     * 远程开锁
     *
     * @param smartLock 智能锁设备对象
     * @return
     */
    @PostMapping(value = "/remoteUnlock")
    public AjaxResult remoteUnlock(@RequestBody SmartLock smartLock) {
        SmartLock smartLock1 = smartLockService.selectBySn(smartLock.getSn());
        if (smartLock1.getBrandId() == SmartLock.THIRD_GENERATION_SMART_LOCK) {
            return error("三代智能锁不支持该功能");
        } else if (smartLock1.getBrandId() == SmartLock.FIVE_GENERATION_SMART_LOCK){
            return success(smartLockHXKEventHandleService.remoteUnlock(smartLock));
        } else if (smartLock1.getBrandId() == SmartLock.FOUR_GENERATION_SMART_LOCK){
            return success(smartLockService.remoteUnlock(smartLock));
        } else {
            return AjaxResult.error("该设备没有远程开锁功能");
        }
    }

    /**
     * 关闭/开启语音播放 状态(0:关闭;1开启)
     *
     * @param smartLock 智能锁设备对象
     * @return
     */
    // todo 空实现，厂商没有这个接口
    @PutMapping(value = "/voiceStatus")
    public AjaxResult voiceStatus(@RequestBody SmartLock smartLock) {
        SmartLock smartLock1 = smartLockService.selectBySn(smartLock.getSn());
        if (smartLock1.getBrandId() == SmartLock.THIRD_GENERATION_SMART_LOCK) {
            return error("三代智能锁不支持该功能");
        } else {
            return toAjax(smartLockService.voiceStatus(smartLock));
        }
    }

    /**
     * 查询亚略特授权用户
     *
     * @param sn 设备号
     * @return
     */
    @GetMapping(value = "/getYLTAuth")
    public AjaxResult getYltAuth(@RequestParam("sn") String sn) {
        return success(smartLockYLTEventService.getYltAuth(sn));
    }


}
